//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <iostream>
#include <fstream>

#include "PearsonCorrelation.h"
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define LENGTHLINE  1000
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool SplitColumn(char* buffer, TListE <String>* columns, char* characters) {

  if (!buffer || !columns || !characters) return false;

  char *part = strtok(buffer, characters);

  while (part) {
    String* cell = new String(part);
    cell->Trim();
    columns->Add(cell);
    part = strtok(NULL, characters);
  }

  return true;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int main(int argc, char** argv) {

  String* filenameData;
  String* filenameOut;

  if (argc < 3) {
    cout << "Error in the input arguments. The execution must be: " << endl;
    cout << "./PearsonCoeff <Input File> <Output File>" << endl;
    return EXIT_FAILURE;
  }
  else {
    cout << "---------------------------------------------------------------------------------" << endl;
    cout << "  Input File : " << argv[1] << endl;
    cout << " Output Name : " << argv[2] << endl;
    cout << "---------------------------------------------------------------------------------" << endl;
    filenameData = new String (argv[1]);
    filenameOut  = new String (argv[2]);
  }

  fstream fileData, fileOut;
  
  fileData.open(filenameData->Out(), fstream::in);
  if (!fileData.is_open()) {
    if (filenameData) delete filenameData;
    if (filenameOut)  delete filenameOut;
    return EXIT_FAILURE;
  }
  
  PearsonCorrelation* pearsonCorrelation = new PearsonCorrelation();
  
  TListE <String>*    columns            = new TListE <String> ();
  
  char                buffer [LENGTHLINE + 1];
  String              line;
  
  while (!fileData.eof()) {
    
    columns->Clear();
    
    fileData.getline(buffer, LENGTHLINE);
    line.In(buffer); line.Trim();
    if (!line.Length()) continue;
    
    SplitColumn(buffer, columns, " \t\n\r");
    if (columns->Length() < 2) continue;
    
    pearsonCorrelation->X()->Add(new double (atof(columns->Get(0)->Out())));
    pearsonCorrelation->Y()->Add(new double (atof(columns->Get(1)->Out())));
    
  }
  
  fileData.close();

  pearsonCorrelation->ComputeCoefficient();
  
  fileOut.open(filenameOut->Out(), fstream::out);
  if (fileOut.is_open()) {
    
    fileOut.precision(6);
    fileOut << "--------------------------------------------------------" << endl;
    fileOut << "Pearson Coefficient : " << pearsonCorrelation->Coefficient()  << endl;
    fileOut << "--------------------------------------------------------" << endl;
    
    cout.precision(6);
    cout << "--------------------------------------------------------" << endl;
    cout << "Pearson Coefficient : " << pearsonCorrelation->Coefficient()  << endl;
    cout << "--------------------------------------------------------" << endl;
    
    fileOut.close();
  }

  if (pearsonCorrelation) delete pearsonCorrelation;
  if (columns)            delete columns;
  if (filenameData)       delete filenameData;
  if (filenameOut)        delete filenameOut;
  
  return EXIT_SUCCESS;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//

